/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package primephysique;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import java.util.Vector;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author AP
 */
public class FindSuitableClass extends javax.swing.JPanel {

    /**
     * Creates new form FindSuitableClass
     */
    public FindSuitableClass() {
        initComponents();
        
    }
private void showResult() {
        try {
            cdb = new ConnectDB();
            con = cdb.getConnection();
            st = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
            String ts=cbbTimeSlot.getSelectedItem().toString();
            String dtn=cbbDay.getSelectedItem().toString();
            // get range of infomation
            if(ts.equalsIgnoreCase("all")) ts="%";
            if(dtn.equalsIgnoreCase("all")) dtn ="%";
            String query = "SELECT TrainingDay,TimeSlot FROM Class WHERE TimeSlot like '"+ts+"%' AND TrainingDay like '"+dtn+"%'";
            
            rs = st.executeQuery(query);
            rsmd = rs.getMetaData();
            Vector<String> columnName = new Vector<String>();
            Vector<String> columnData;
            for (int i = 0; i < rsmd.getColumnCount(); i++) {
                columnName.add(rsmd.getColumnName(i + 1));
            }
            // set table column's Name
            model = new DefaultTableModel();
            int cc = rsmd.getColumnCount();
            for (int i = 0; i < cc; i++) {
                model.addColumn(columnName.get(i));
            }

            while (rs.next()) {
                columnData = new Vector<String>();
                for (int i = 0; i < cc; i++) {
                    columnData.add(rs.getString(i + 1));
                    //     System.out.println(rs.getString(i + 1));
                }
                model.addRow(columnData);
                //         columnData.clear();
            }
            tbResult.setModel(model);
            scpResult.setViewportView(tbResult);
            con.close();


        } catch (Exception ex) {

            ex.printStackTrace();
        }
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnlSearch = new javax.swing.JPanel();
        cbbTimeSlot = new javax.swing.JComboBox();
        lbTimeSlot = new javax.swing.JLabel();
        lbDay = new javax.swing.JLabel();
        btnSearch = new javax.swing.JButton();
        cbbDay = new javax.swing.JComboBox();
        pnlResult = new javax.swing.JPanel();
        scpResult = new javax.swing.JScrollPane();
        tbResult = new javax.swing.JTable();

        setLayout(new java.awt.BorderLayout());

        pnlSearch.setPreferredSize(new java.awt.Dimension(400, 90));

        cbbTimeSlot.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "All", "9h-11h", "17h-19h", " " }));

        lbTimeSlot.setText("Time Slot");

        lbDay.setText("Day");

        btnSearch.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/Capture.PNG"))); // NOI18N
        btnSearch.setText("Search");
        btnSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSearchActionPerformed(evt);
            }
        });

        cbbDay.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "All", "3-5-7", "2-4-6" }));

        javax.swing.GroupLayout pnlSearchLayout = new javax.swing.GroupLayout(pnlSearch);
        pnlSearch.setLayout(pnlSearchLayout);
        pnlSearchLayout.setHorizontalGroup(
            pnlSearchLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlSearchLayout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addGroup(pnlSearchLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lbTimeSlot)
                    .addComponent(lbDay))
                .addGap(28, 28, 28)
                .addGroup(pnlSearchLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(cbbTimeSlot, 0, 111, Short.MAX_VALUE)
                    .addComponent(cbbDay, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addComponent(btnSearch)
                .addGap(89, 89, 89))
        );
        pnlSearchLayout.setVerticalGroup(
            pnlSearchLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlSearchLayout.createSequentialGroup()
                .addGap(11, 11, 11)
                .addGroup(pnlSearchLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnlSearchLayout.createSequentialGroup()
                        .addGroup(pnlSearchLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(pnlSearchLayout.createSequentialGroup()
                                .addGap(3, 3, 3)
                                .addComponent(lbTimeSlot))
                            .addComponent(cbbTimeSlot))
                        .addGap(10, 10, 10)
                        .addGroup(pnlSearchLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lbDay)
                            .addComponent(cbbDay)))
                    .addGroup(pnlSearchLayout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(btnSearch)))
                .addGap(29, 29, 29))
        );

        add(pnlSearch, java.awt.BorderLayout.PAGE_START);

        tbResult.setModel(model
        );
        scpResult.setViewportView(tbResult);

        javax.swing.GroupLayout pnlResultLayout = new javax.swing.GroupLayout(pnlResult);
        pnlResult.setLayout(pnlResultLayout);
        pnlResultLayout.setHorizontalGroup(
            pnlResultLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
            .addGroup(pnlResultLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(pnlResultLayout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(scpResult, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
                    .addContainerGap()))
        );
        pnlResultLayout.setVerticalGroup(
            pnlResultLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 320, Short.MAX_VALUE)
            .addGroup(pnlResultLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlResultLayout.createSequentialGroup()
                    .addContainerGap(23, Short.MAX_VALUE)
                    .addComponent(scpResult, javax.swing.GroupLayout.PREFERRED_SIZE, 286, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap()))
        );

        add(pnlResult, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents

    private void btnSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSearchActionPerformed
        // TODO add your handling code here:
        showResult();
    }//GEN-LAST:event_btnSearchActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnSearch;
    private javax.swing.JComboBox cbbDay;
    private javax.swing.JComboBox cbbTimeSlot;
    private javax.swing.JLabel lbDay;
    private javax.swing.JLabel lbTimeSlot;
    private javax.swing.JPanel pnlResult;
    private javax.swing.JPanel pnlSearch;
    private javax.swing.JScrollPane scpResult;
    private javax.swing.JTable tbResult;
    // End of variables declaration//GEN-END:variables
 private ConnectDB cdb = null;
    private Connection con;
    private Statement st;
    private ResultSet rs;
    private ResultSetMetaData rsmd;
    private DefaultTableModel model = new DefaultTableModel();
}
